package com.niubility.other;

import com.qiniu.http.Response;
import org.omg.CORBA.Request;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.awt.SunHints;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@RestController

public class FaceUpLoad {
    private static QiniuUtilService qiniuUtilService;

    public FaceUpLoad(QiniuUtilService qiniuUtilService) throws IOException {
        FaceUpLoad.qiniuUtilService = qiniuUtilService;
    }
    private static String savePath="D:\\QNY\\src\\main\\webapp\\Image";

    @RequestMapping(value="http://111.19.45.22:8888//QNY/FaceUpLoad",method= RequestMethod.POST )
    public static String FaceUpLoad(HttpServletRequest request, HttpServletResponse response) throws IOException {

      try {

          InputStream inputStream=con.getInputStream();
          byte[] getDate= readInputStream(inputStream);
          File saveDir = new File(savePath);
          if (!saveDir.exists()) {
              saveDir.mkdir();
          }
          File file = new File(saveDir + File.separator + fileName);
          FileOutputStream fos = new FileOutputStream(file);
          fos.write(getDate);
          //String Path=savePath+"\\"+fileName;
          //qiniuUtilService.upload(Path);
          if (fos != null) {
              fos.close();
          }
          if (inputStream != null) {
              inputStream.close();
          }
          // System.out.println("info:"+url+" download success");


      } catch (IOException e) {
          e.printStackTrace();
      }
      String Path= savePath + "\\" + fileName;

      return qiniuUtilService.upload(Path);
  }

    private static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}
